<template>
  <div class="login-container">
    <el-row>
      <el-col :lg="12" :md="11" :sm="24" :xl="14" :xs="24">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :lg="11" :md="12" :sm="24" :xl="9" :xs="24">
        <el-form
          ref="formRef"
          class="login-form"
          label-position="left"
          :model="loginForm"
          :rules="loginRules"
        >
          <div class="title">hello !</div>
          <div class="title-tips">
            {{ '欢迎来到' }}{{ ' CRM客户关系管理系统' }}！
          </div>
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginForm.username"
              placeholder='请输入用户名'
              tabindex="1"
              type="text"
            >
              <template slot="prepend">
                <span class="svg-container">
                  <svg-icon icon-class="user"/>
                 </span>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="passwordRef"
              v-model.trim="loginForm.password"
              placeholder='请输入密码'
              tabindex="2"
              :type="passwordType"
              @keyup.enter="handleLogin"
            >
              <template slot="prepend">
                <svg-icon icon-class="password"/>
              </template>
            </el-input>
            <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
          </el-form-item>
          <el-form-item>
            <el-button
              class="login-btn"
              :loading="loading"
              type="primary"
              @click="handleLogin"
              @keyup.enter="keyDown(e)"
            >
              {{ '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :lg="1" :md="1" :sm="24" :xl="1" :xs="24">
        <div style="color: transparent">占位符</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {validUsername} from '@/utils/validate'
import $ from "@/api/assets";
import router from "@/router";

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能少于 3 位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        // console.log(route)
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    // 绑定监听事件
    window.addEventListener("keydown", this.keyDown);
  },
  destroyed() {
    // 销毁事件
    window.removeEventListener("keydown", this.keyDown, false);
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      // this.$nextTick(() => {
      //   this.$refs.password.focus()
      // })
    },
    handleLogin() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({path: this.redirect || '/'})
            this.loading = false

          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 点击回车键登录
    keyDown(e) {
      // 回车则执行登录方法 enter键的ASCII是13
      if (e.keyCode == 13 || e.keyCode == 100) {
        this.handleLogin(); // 定义的登录方法
      }
    },

  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  background: url('~@/assets/login_images/background.jpg') center center fixed no-repeat;
  background-size: cover;
}

.login-form {
  position: relative;
  max-width: 100%;
  padding: 4.5vh;
  margin: calc((100vh - 555px) / 2) 5vw 5vw;
  overflow: hidden;
  background: url('~@/assets/login_images/login_form.png');
  background-size: 100% 100%;

  .title {
    font-size: 54px;
    font-weight: 500;
    color: #ffffff;
  }

  .title-tips {
    margin-top: 29px;
    font-size: 26px;
    font-weight: 400;
    color: #ffffff;
  }

  .login-btn {
    display: inherit;
    width: 100%;
    height: 45px;
    margin-top: 5px;
    background: #409eff;
    border: 0;
    font-size: 18px;

    &:hover {
      opacity: 0.9;
    }
  }

  .tips {
    margin-bottom: 10px;
    font-size: 14px;
    color: #ffffff;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .title-container {
    position: relative;

    .title {
      margin: 0 auto 40px auto;
      font-size: 34px;
      font-weight: bold;
      color: #409eff;
      text-align: center;
    }
  }

  i {
    position: absolute;
    top: 8px;
    left: 15px;
    font-size: 16px;
  }

  .show-password {
    float: right;
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  .el-form-item {
    padding-right: 0;
    margin: 20px 0;
    color: #454545;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 2px;

    &__content {
      min-height: 32px;
      line-height: 32px;
    }

    &__error {
      position: absolute;
      top: 100%;
      left: 18px;
      font-size: 12px;
      line-height: 18px;
      color: #f56c6c;
    }
  }

  .el-input {
    box-sizing: border-box;

    ::v-deep input {
      height: 46px;
      padding-left: 30px;
      line-height: 48px;
      border: 0;
    }

    &__suffix-inner {
      position: absolute;
      right: 65px;
      cursor: pointer;
    }
  }

  .code {
    position: absolute;
    top: 4px;
    right: 4px;
    cursor: pointer;
    border-radius: 2.5px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: gray;
    cursor: pointer;
    user-select: none;
  }

  .svg-container {
    padding: 6px 5px 6px 0px;
    color: gray;
    vertical-align: middle;
    width: 10px;
    display: inline-block;
  }

}
</style>
